package cn.seqdata.jackson;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import cn.seqdata.jpa.JpaAuditableObject;

public class ObjectSerializer {
	public static class Id extends StdSerializer<JpaAuditableObject> {
		private static final long serialVersionUID = 1L;

		public Id() {
			super(JpaAuditableObject.class);
		}

		@Override
		public void serialize(JpaAuditableObject value, JsonGenerator jgen, SerializerProvider provider)
				throws IOException {
			if (null != value.getId()) {
				jgen.writeString(Long.toString(value.getId()));
			} else {
				jgen.writeNull();
			}
		}
	}

	public static class IdName extends StdSerializer<JpaAuditableObject> {
		private static final long serialVersionUID = 1L;

		public IdName() {
			super(JpaAuditableObject.class);
		}

		@Override
		public void serialize(JpaAuditableObject value, JsonGenerator jgen, SerializerProvider provider)
				throws IOException {
			jgen.writeStartObject();
			if (null != value.getId()) {
				jgen.writeStringField("id", Long.toString(value.getId()));
			} else {
				jgen.writeNullField("id");
			}
			jgen.writeStringField("name", value.getName());
			jgen.writeEndObject();
		}
	}
}
